package cn.yaboya.controller;

import cn.yaboya.domain.User;
import cn.yaboya.sentinel.GlobaFlowMethodFactory;
import cn.yaboya.sentinel.fallbackMethodFactory;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Reference // 刷新配置
@RestController
public class UserController {

    @Value("${temp.notify}")
    private String notify;

    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "user",blockHandler = "exceptionHandler",
            blockHandlerClass = GlobaFlowMethodFactory.class,
            fallback = "fallbackMethod")//, fallbackClass = fallbackMethodFactory.class
    public User getById(@PathVariable Long id){
        System.out.println("测试配置notify=" + notify);
        int a = 1/0;
        return new User(id,"zs:"+id, "我是zs");
    }

//    // 限流与阻塞处理 : 参数要和 被降级的方法参数一样
//    public static User exceptionHandler(@PathVariable Long id, BlockException ex){
//        ex.printStackTrace();
//        System.out.println("限流了");
//        return new User(-1L, "限流了","限流了");
//    }
//
//    // 熔断降级，参数和返回值与源方法一致
    public User fallbackMethod(@PathVariable Long id){
        System.out.println(notify);
        return new User(id,"zs:"+id, "熔断托底了");
    }

}
